// pages/fileprint/print/print.js
const app=getApp()
Page({

 
  data: {   
    url:'',number:1,speclist:[],goods_id:"",near_id:'',arr2:['左上','左中','左下','中上','中','中下','右上','右中','右下'],index2:4,
  typeobj:{},radio:1,radio1:'',page:"",name:"",price:'',man:'',jian:'',peisong:1,code:"",coupon_id:'',filepath:'',
  appId:'',value1:'1',value2:'',spec_id:'',basepage:'',
  nonceStr:'',address:'',address_name:'',address_phone:'', show: false,passwordvalue:'',money:'',name1:'',time:'',address_id:'',radio5:3,radio6:1,
  suofang:'',type:3,sheets:'',i:'',basesheets:'',flag:'',last:'',show1:false,flag:false,flag1:false,flag2:false,arr5:['拉伸全图','自适应','自定义'],index5:0,
   number1:100,param:'',show3:'',photo:'',param1:'',radio7:1,
  package:'',
  paySign:'',
  signType:'',
  timeStamp:'',value5:'',typearr:'',icon:false,taocanindex:0,onshow:true
  

  },
 
  
  onLoad: function (options) {
    

   



    if(options.index2){
      this.setData({index2:options.index2})
    }
    if(options.param1){
      this.setData({param1:1})
    }
    if(options.flag==17){
      this.setData({flag:17})
    }
    if(options.typearr){
        this.setData({
          typearr:1,
          flag2:1,
          param:1
        })
    }
    if(options.photo){
      this.setData({photo:options.photo})
    }
    if(options.param){
      this.setData({param:1})
    }
    if(options.flag2){
      this.setData({flag2:true})
    }
    if(options.flag1){
      this.setData({flag1:true})
    }
    console.log(options);
    console.log(options.radio5);
    if(options.radio5){
      console.log('执行了');
      
      this.setData({radio5:options.radio5})
    }
    
    if(options.near_id!=='undefined'){
    this.setData({
      near_id:options.near_id,
      goods_id:options.goods_id
    })}

    
   
    console.log(options);
    
    if(options.name&&this.data.goods_id!=''&&options.page>1){this.getpage(options.name);
    }
    
    
   let name1=options.filename?options.filename:''
  let filepath=options.filepath?options.filepath:''
  console.log(name1);
  let url=options.url?options.url:''
  if(url!=''&&url.substring(0,5)!="https"){
     url="https://zujieba.com/uploads/"+url

  }
  console.log(url);
  let page=options.page?options.page:''
  let sheets=options.page?options.page:''
  let name=options.name?options.name:''
  console.log(page);
  
    this.setData({filepath,url,page,name,value2:page,name1,time:options.time,sheets,basepage:page})
    console.log(this.data.name1);
    
    if(this.data.goods_id!=''){
      
      this.getrulelist(options.param1)
    }
    console.log(this.data.url);
    this.getpages(1)
  },
  //接受goods_id
   onShow(){
    //  点击了预览或者 微信支付 不让重新选规格
     
     this.getaddress()
     console.log(this.data.url);
     
     console.log(this.data.near_id);
     this.gettaocan()
     
     console.log(this.data.goods_id);
     if(this.data.goods_id!=''){
      
     
     this.getpeisong()
     
     
     
     
     }
     if(this.data.goods_id!=''&&this.data.show3!=1){
       this.getrulelist()
     }
   },
   //图片预览的接口
    getimg(status,type){
      let jppagerange=this.data.value1+"_"+this.data.value2
      let jpautoscale=this.data.radio5
      if(this.data.radio5==3){
        jpautoscale=3
      }else if(this.data.radio5==4){
        jpautoscale=4
      }else if(this.data.radio5==6){
        jpautoscale=this.data.number1+"%"
      }
      let jpautoalign
      if(this.data.index2==0){
         jpautoalign='z1'
      }else if(this.data.index2==1){
       jpautoalign='z4'
      }else if(this.data.index2==2){
       jpautoalign='z7'
      }else if(this.data.index2==3){
       jpautoalign='z2'
      }else if(this.data.index2==4){
       jpautoalign='z5'
      }else if(this.data.index2==5){
       jpautoalign='z8'
      }else if(this.data.index2==6){
       jpautoalign='z3'
      }else if(this.data.index2==7){
       jpautoalign='z6'
      }else if(this.data.index2==8){
       jpautoalign='z9'
      }
        wx.request({
          url: 'https://zujieba.com/Api/test2/show.html',
          method:"POST",
          data:{goodsid:this.data.goods_id,spec_id:this.data.spec_id,dmorientation:this.data.radio6,dmcopies:this.data.number,jpautoscale,jpautoalign,file:this.data.url,status,type,jppagerange,jptype:this.data.type},
          success:(res)=>{
            console.log(res);
            if(res.data.success==1){
              this.getimg1(1,2)
            }else{
              wx.hideLoading()
              wx.showToast({
                title: res.data.data.msg,
                icon:'none',
                mask:true
              })
            }
            
          }
        })
    },
    getimg1(status,type){
      
      let jppagerange=this.data.value1+"_"+this.data.value2
      let jpautoscale=this.data.radio5
      if(this.data.radio5==3){
        jpautoscale=3
      }else if(this.data.radio5==4){
        jpautoscale=4
      }else if(this.data.radio5==6){
        jpautoscale=this.data.number1+"%"
      }
      let jpautoalign
      if(this.data.index2==0){
         jpautoalign='z1'
      }else if(this.data.index2==1){
       jpautoalign='z4'
      }else if(this.data.index2==2){
       jpautoalign='z7'
      }else if(this.data.index2==3){
       jpautoalign='z2'
      }else if(this.data.index2==4){
       jpautoalign='z5'
      }else if(this.data.index2==5){
       jpautoalign='z8'
      }else if(this.data.index2==6){
       jpautoalign='z3'
      }else if(this.data.index2==7){
       jpautoalign='z6'
      }else if(this.data.index2==8){
       jpautoalign='z9'
      }
        wx.request({
          url: 'https://zujieba.com/Api/test2/show.html',
          method:"POST",
          data:{goodsid:this.data.goods_id,spec_id:this.data.spec_id,dmorientation:this.data.radio6,dmcopies:this.data.number,jpautoscale,jpautoalign,file:this.data.url,status,type,jppagerange,jptype:this.data.type},
          success:(res)=>{
            console.log(res,this.data.url);
            if(res.data.success==1){
                this.getimg2(1,3,res.data.data.data.task_id)
                
            }else{
              wx.hideLoading()
              wx.showToast({
                title: res.data.tips,
                mask:true
              })
            }
            
          }
        })
    },
   
    getimg2(status,type,task_id){
      console.log(this.data.index5);
      console.log(this.data.radio5);
      
      let jppagerange=this.data.value1+"_"+this.data.value2
      let jpautoscale=this.data.radio5
      if(this.data.radio5==3){
        jpautoscale=3
      }else if(this.data.radio5==4){
        jpautoscale=4
      }else if(this.data.radio5==6){
        jpautoscale=this.data.number1+"%"
      }
      let jpautoalign
      if(this.data.index2==0){
         jpautoalign='z1'
      }else if(this.data.index2==1){
       jpautoalign='z4'
      }else if(this.data.index2==2){
       jpautoalign='z7'
      }else if(this.data.index2==3){
       jpautoalign='z2'
      }else if(this.data.index2==4){
       jpautoalign='z5'
      }else if(this.data.index2==5){
       jpautoalign='z8'
      }else if(this.data.index2==6){
       jpautoalign='z3'
      }else if(this.data.index2==7){
       jpautoalign='z6'
      }else if(this.data.index2==8){
       jpautoalign='z9'
      }
      let params={goodsid:this.data.goods_id,spec_id:this.data.spec_id,dmorientation:this.data.radio6,dmcopies:this.data.number,jpautoscale,jpautoalign,file:this.data.url,status,type,jppagerange,task_id,jptype:this.data.type}
      console.log(params);
      
        wx.request({
          url: 'https://zujieba.com/Api/test2/show.html',
          method:"POST",
          data:{goodsid:this.data.goods_id,spec_id:this.data.spec_id,dmorientation:this.data.radio6,dmcopies:this.data.number,jpautoscale,jpautoalign,file:this.data.url,status,type,jppagerange,task_id,jptype:this.data.type},
          success:(res)=>{
            console.log(res);
            if(res.data.success==1){
                wx.hideLoading()
                if(res.data.data.data.task_result.code!=200){
                  wx.showToast({
                    title: res.data.data.data.task_result.msg,
                    icon:'none',
                    mask:true
                  })
                  return
                }
                let url=res.data.data.data.task_result.data.img_list[0]
                console.log(url);
              
                  wx.previewImage({
                    urls: res.data.data.data.task_result.data.img_list,
                  })
                
              
             
                
                
                
                
               
               
            }else{
              wx.hideLoading()
              wx.showToast({
                title: res.data.tips,
                mask:true
              })
            }
            
          }
        })
    },
   getpages(direction){
     wx.showLoading({
       title: '获取页数',
       mask:true
     })
    let goods_id
    if(this.data.goods_id){
         goods_id=this.data.goods_id
    }else{
       goods_id=''
    }
   
   
     wx.request({
       url: 'https://zujieba.com/Api/test2/printing.html',
       data:{goods_id:goods_id,file:this.data.name,dmOrientation:direction},
       success:(res2)=>{
         wx.hideLoading()
          if(res2.data.success==1){
            this.setData({
              page:res2.data.data.data.pages,
              value2:res2.data.data.data.pages,
             
            })
            this.toggle14()
          }
         
         else{
            wx.showToast({
              title: res2.data.data.msg,
            })
         }
         console.log(res2);
         
         /*let imgurl=res2.data.data.data.task_result.data.img_list
         let page=imgurl.length
         app.globalData.arr=imgurl*/
        
        
       },
       fail:(res)=>{
         console.log(res);
         wx.hideLoading()
       }
       

     })
   },
    changeinput(e){
      console.log(e);
      
        console.log(typeof(e.detail.value));
        let abc=Number(e.detail.value)
        console.log(typeof(abc),abc);
        
        
            this.setData({
               number1:abc
            })  
    },
  //预览
  preview(){
    this.setData({
      onshow:false
    })
    console.log(this.data.url);
    console.log(this.data.typearr,app.globalData.arr);
    
   

    if(this.data.goods_id!=''){
      this.setData({show3:1})
    }

    if(!this.data.filepath&&this.data.goods_id){
      wx.showLoading({
        title: '正在打开',
      })
      this.throttle(()=>{this.getimg(1,1)},1500)
      
    }
    else if(this.data.param==1&&!this.data.goods_id){
      console.log(333);
      
      wx.downloadFile({
        url: this.data.url,
        success:(res)=>{
          console.log(res);
          let file=res.tempFilePath
          wx.openDocument({
            filePath: file,
            
          })
        }
      })
    }else if(!this.data.filepath&&!this.data.goods_id&&this.data.flag2!=1){
      console.log(222);
     
      wx.previewImage({
        current: this.data.url,
        urls: [this.data.url]
      })
    } else if(this.data.typearr==1&&!this.data.goods_id){
      console.log(555);
      
      wx.previewImage({
        urls: app.globalData.arr,
      })
    }else{
      console.log(4444);
      
      let url=this.data.url
      wx.downloadFile({
        url: url,
        success:(res)=>{
          wx.openDocument({
            filePath: res.tempFilePath,
          })
        }
      })
    }
    
    
     
    
  /*  if(this.data.filepath!=''){
      console.log(1111,this.data.filepath);
      wx.downloadFile({
        url: this.data.filepath,
        success:(res)=>{
          console.log(res);
          let file=res.tempFilePath
          wx.openDocument({
            filePath: file,
            
          })
        }
      })
     
    }else if(this.data.url!='' && !this.data.filepath&&this.data.flag2==false){
      console.log(22222);
      wx.previewImage({
        current: this.data.url,
        urls: [this.data.url]
      })
    }else{
      let url=this.data.url
      wx.downloadFile({
        url: url,
        success:(res)=>{
          wx.openDocument({
            filePath: res.tempFilePath,
          })
        }
      })
      
    }*/
  
    
  },
  jianclick(){
    console.log(1);
    
    let value=this.data.number
    
    if(value>1){
      value--
      this.setData({
        number:value
      })
      
    }
    let price=this.data. price*this.data.number*(this.data.sheets?this.data.sheets:1)+(this.data.radio==2?1:0)
    if(price<this.data.man){
       this.setData({
         jian:0
       })
    }
  },
  addclick(){
    let value=this.data.number
    value++
    this.setData({
      number:value
    })
    let price=this.data. price*this.data.number*(this.data.sheets?this.data.sheets:1)+(this.data.radio==2?1:0)
    if(price<this.data.man){
       this.setData({
         jian:0
       })
    }
  },
  //获取规格数据
  getrulelist(){
    
    
    wx.request({
      url: 'https://zujieba.com/Api/spec/spec',
      data:{id:this.data.goods_id},
      method:"POST",
      success:(res)=>{
      console.log(res);
      
      
      
      const speclist=res.data.data.spec ||[]
      speclist.forEach((item,index)=>{
        
            if(index==0){
              item.data.forEach((item1)=>{
                if(this.data.param1==1||this.data.index2==0){
                  if(item.data.length==2){

                  
                  if(item1.title=="彩色"){    
                    item1.checked=true
                }}else{
                  item1.checked=true
                }
              
                }else{
                  if(item1.title=="黑白"){
                    item1.checked=true
                }
                }
               
              })
            }
            if(index==1){
              item.data.forEach((item1)=>{
                if(item1.title=="单面"){
                    item1.checked=true
                }
              })
            }
            if(index==2){
              item.data.forEach((item1)=>{
                if(item1.title=="A4"){
                    item1.checked=true
                }
              })
            }
            if(index==3){
              item.data.forEach((item1)=>{
                if(item1.title=="普通打印"){
                    item1.checked=true
                }
              })
            }
            this.setData({speclist})
      })

      
      
      this.getmoney()
      
      
      }
    })
  },
  //点击规格切换
  click(e){
    if(this.data.photo==1&&e.currentTarget.dataset.index==1){
      return
    }
    if(this.data.page==1&&e.currentTarget.dataset.index==1){
      return
    }
    if(this.data.photo!=1&&e.currentTarget.dataset.index==3){
      return
    }
    console.log(e);
      let speclist=this.data.speclist
     let index=  this.data.speclist[e.currentTarget.dataset.index].data.findIndex(item=>{
         return item.checked
       })
        speclist[e.currentTarget.dataset.index].data[index].checked=false
       speclist[e.currentTarget.dataset.index].data[e.currentTarget.dataset.i].checked=true
       this.setData({
           speclist,
           i:e.currentTarget.dataset.i
       })
       
    
    
    
    if(e.currentTarget.dataset.index==0){
      this.setData({
         a:e.currentTarget.dataset.id
      })
      
   }else{
     if(e.currentTarget.dataset.index==1){
       this.setData({
         b:e.currentTarget.dataset.id
       })
     }else{this.setData({
       c:e.currentTarget.dataset.id
     })}
   }   
   this.getmoney()
   /*let arr=this.data.speclist[1].data
   var title
  arr.forEach((item)=>{
     if(item.checked==true){
       title=item.title
     }
  })
  if(this.data.flag==2){
     if(title=='长边'||title=='短边'){
        return
     }else{
       let sheets1=this.data.basesheets
        this.setData({
          sheets:sheets1
        })
     }
  }
  if(this.data.flag==1){
    if(title=='长边'||title=='短边'){
       if(this.data.sheets%2==0){
          this.setData({sheets:this.data.sheets/2})
       }else{
         this.setData({
           sheets:parseInt(this.data.sheets/2)+1
         })
       }
    }else{
       this.setData({
         sheets:this.data.basesheets
       })
    }
 }*/
 
  
  
  },
  bindPickerChange5(e){
    console.log(e);
    
    this.setData({index5:e.detail.value})
    if(e.detail.value==0){
      this.setData({
         radio5:3
      })
    }
    if(e.detail.value==1){
      this.setData({
         radio5:4
      })
    }
    if(e.detail.value==2){
      this.setData({
         radio5:6
      })
    }
  },

  //获取配送方式
  getpeisong(){
    wx.request({
      url: 'https://zujieba.com/Api/store/type.html',
      data:{pid:this.data.near_id},
      success:(res)=>{
        console.log(typeof(this.data.near_id));
        
        console.log(res);
        //tp dema package
       let typeobj= res.data.data
       this.setData({
           typeobj
       })
       this.getyuemoney()
      
  }
})
  },
  //跳转到店铺列表
    navnear(){
       wx.navigateTo({
         url: '/pages/index/near/near',
       })
    },
    //跳转优惠券
     navcoupon(){
       const spec_id=this.data.spec_id
       wx.navigateTo({
         url: '/pages/mine/newcoupon?store_id='+this.data.near_id+"&goods_id="+this.data.goods_id+"&spec_id="
         +spec_id+"&page="+this.data.sheets+"&number="+this.data.number,
       })
       
     },
     toggle(e){
       
       let radio=e.currentTarget.dataset.radio
       this.setData({
         radio
       })
      
         this.getaddress()
       
         
     },
     toggle1(e){
       let radio1=e.currentTarget.dataset.radio1
       
       this.setData({
         radio1,
         icon:false
        
       })
     },
     toggle2(e){
       console.log(e);
       
        let icon=this.data.icon
        this.setData({
          icon:!icon,
          radio1:e.currentTarget.dataset.radio1
        })
     },
     //获取规格价钱
     getmoney(){
       let arr9=[]
       this.data.speclist.forEach((item,index)=>{
          item.data.forEach((item1)=>{
            if(item1.checked==true)
            {arr9.push(item1.id)}
             
          })
       })
    let str=arr9.join('_')
       console.log(arr9,str);
       this.setData({spec_id:str})
       const spec_id=str
       const  number=this.data.number
       const goods_id=this.data.goods_id
       const params={spec_id,goods_id,number}
         wx.request({
           url: 'https://zujieba.com/Api/spec/spec_price.html',
           data:params,
           method:'POST',
           success:(res)=>{
             console.log(res);
             console.log(params);
             
             let price=res.data.data
             this.setData({
               price
             })
             
           }
         })
     },
     payclick(){
       console.log(this.data.radio5);
       
       console.log(this.data.url);
       
       this.throttle(()=>{

        let jpautoalign
        if(this.data.index2==0){
           jpautoalign='z1'
        }else if(this.data.index2==1){
         jpautoalign='z4'
        }else if(this.data.index2==2){
         jpautoalign='z7'
        }else if(this.data.index2==3){
         jpautoalign='z2'
        }else if(this.data.index2==4){
         jpautoalign='z5'
        }else if(this.data.index2==5){
         jpautoalign='z8'
        }else if(this.data.index2==6){
         jpautoalign='z3'
        }else if(this.data.index2==7){
         jpautoalign='z6'
        }else if(this.data.index2==8){
         jpautoalign='z9'
        }
       console.log(this.data.url);
     /*  let jpautoscale
          if(this.data.radio5==2){
             jpautoscale=this.data.suofang+'%'
           
          }else{
            jpautoscale=this.data.radio5
          }
          console.log(jpautoscale);*/
          
          let sheets
          if(this.data.name&&this.data.page>1){this.getpage(this.data.name)}
          if(this.data.page==''||this.data.page==1){sheets=1}else if(this.data.type==1){
            if(this.data.page%2==0){
              sheets=this.data.page/2
              console.log(1111);
              
            }else{
             sheets=parseInt(this.data.page/2)+1
             console.log(222);
             
            }
          }else if(this.data.type==2){
               
                 sheets=parseInt(this.data.page/2)
                 console.log(333);
                 
               
   
          }else{sheets=this.data.value2-this.data.value1+1}
          console.log(sheets);
          let radio5=this.data.radio5
          
          if(this.data.radio5==3){
             radio5=this.data.number1+"%"
          }
          console.log(this.data.radio5);
          

          
         const spec_id=this.data.spec_id
         const uid=wx.getStorageSync('uid')
         const goods_id=this.data.goods_id
         const file=this.data.url
         const store_id=this.data.near_id
         const pay=this.data.radio1 
         const dmcopies=this.data.number
         const deli=this.data.radio
         const coupon_id=this.data.coupon_id
          const address_id=this.data.address_id
         const params={uid,goods_id,spec_id,coupon_id,pay,dmcopies,deli,file,sheets,address_id,jpautoalign,dmorientation:this.data.radio6,jpautoscale:radio5}
         var that =this
         wx.request({
           url: 'https://zujieba.com/Api/order/add_order.html',
           data:params,
           method:'POST',
           success:(res)=>{
             console.log(res);
             if(res.data.success==1){
             that.setData({
               code:res.data.data
             })
             console.log(res.data.data);
             
          
            wx.request({
              url: 'https://zujieba.com/Api/order/pay_order.html',
              data:{code:res.data.data},
              success:(res)=>{
                if(this.data.radio1==1){
                  console.log(1111,res);
                  if(res.success!='2'){
                   that.setData({
                     appId:res.data.appId,
                     nonceStr:res.data.nonceStr,
                     package:res.data.package,
                     paySign:res.data.paySign,
                     signType:res.data.signType,
                     timeStamp:res.data.timeStamp,
                     
                   })
                   that.getpay()
                  }else{wx.showToast({
                    title: "失败",
                    mask:true
                  })}
               
                }else{
                  let price=this.data. price*this.data.number*(this.data.sheets?this.data.sheets:1)+(this.data.radio==2?1:0)-(this.data.jian?this.data.jian:0)
                  console.log(price,this.data.money);
                  if(price>this.data.money){
                     return  wx.showToast({
                       title: '余额不足',
                       mask:true
                     })
                  }
                  that.getmima()
                 
                }
                
               
                
              }
            })
            
             
           }else{
             wx.showToast({
               title: res.data.tips,
               icon:'none',
               mask:true
             })
           }
          
          }
          
      
         })
       },1500)


     },
     payclick1(){
       this.setData({
         onshow:false
       })
      console.log(this.data.radio5,this.data.number1);
      
      console.log(this.data.url);
      
      this.throttle(()=>{

       let jpautoalign
       if(this.data.index2==0){
          jpautoalign='z1'
       }else if(this.data.index2==1){
        jpautoalign='z4'
       }else if(this.data.index2==2){
        jpautoalign='z7'
       }else if(this.data.index2==3){
        jpautoalign='z2'
       }else if(this.data.index2==4){
        jpautoalign='z5'
       }else if(this.data.index2==5){
        jpautoalign='z8'
       }else if(this.data.index2==6){
        jpautoalign='z3'
       }else if(this.data.index2==7){
        jpautoalign='z6'
       }else if(this.data.index2==8){
        jpautoalign='z9'
       }
      console.log(this.data.url);
    /*  let jpautoscale
         if(this.data.radio5==2){
            jpautoscale=this.data.suofang+'%'
          
         }else{
           jpautoscale=this.data.radio5
         }
         console.log(jpautoscale);*/
         
         let sheets
         
         if(this.data.page==''||this.data.page==1){sheets=1}else if(this.data.type==1){
           if(this.data.page%2==0){
             sheets=this.data.page/2
             console.log(1111);
             
           }else{
            sheets=parseInt(this.data.page/2)+1
            console.log(222);
            
           }
         }else if(this.data.type==2){
              
                sheets=parseInt(this.data.page/2)
                console.log(333);
                
              
  
         }else{sheets=this.data.value2-this.data.value1+1}
         console.log(sheets);
         let radio5=this.data.radio5
        
         console.log(this.data.number1);
         let num = this.data.number1
         if(this.data.radio5==6){
           if(this.data.number1>100){
              radio5='100%'
           }else if(this.data.number1=0||this.data.number1==''){
              radio5="100%"
           }
           else{
             console.log(num,this.data.number1);
              radio5=num+'%'
           }
           
            
         }
         this.setData({
           number1:num
         })
         console.log(this.data.number1,num,radio5);
         var jppagerange
        var   jptype
        if(this.data.name&&this.data.page>1){
          jppagerange=this.data.value1+'_'+this.data.value2
          jptype =this.data.type
        }else{
          jppagerange=''
          jptype =''
        }
          
        let priceid=''
        if(this.data.radio1==3){
           priceid=this.data.taocan[this.data.taocanindex].id
        }
        const spec_id=this.data.spec_id
        const uid=wx.getStorageSync('uid')
        const goods_id=this.data.goods_id
        const file=this.data.url
        const store_id=this.data.near_id
        const pay=this.data.radio1 
        const dmcopies=this.data.number
        const deli=this.data.radio
        const coupon_id=this.data.coupon_id
         const address_id=this.data.address_id
        const params={uid,goods_id,spec_id,coupon_id,jppagerange,pay,jptype,dmcopies,deli,file,sheets,address_id,jpautoalign,dmorientation:this.data.radio6,jpautoscale:radio5,page:this.data.page,now:this.data.radio7,priceid}
        console.log(params);
        
        var that =this
        wx.request({
          url: 'https://zujieba.com/Api/order/new_add_order.html',
          data:params,
          method:'POST',
          success:(res)=>{
            console.log(res);
            if(res.data.success==1){
            that.setData({
              code:res.data.data
            })
            console.log(res.data.data);
            
         
           wx.request({
             url: 'https://zujieba.com/Api/order/pay_order.html',
             data:{code:res.data.data},
             success:(res)=>{
               if(that.data.radio1==1){
                 console.log(1111,res);
                 if(res.success!='2'){
                  that.setData({
                    appId:res.data.appId,
                    nonceStr:res.data.nonceStr,
                    package:res.data.package,
                    paySign:res.data.paySign,
                    signType:res.data.signType,
                    timeStamp:res.data.timeStamp,
                    
                  })
                  that.getpay()
                 }else{wx.showToast({
                   title: "失败",
                   mask:true
                 })}
              
               }else if(this.data.radio1==2){
                 let price=that.data. price*that.data.number*(that.data.sheets?that.data.sheets:1)+(that.data.radio==2?1:0)-(that.data.jian?thats.data.jian:0)
                 console.log(price,that.data.money);
                 if(price>that.data.money){
                    return  wx.showToast({
                      title: '余额不足',
                      mask:true
                    })
                 }
                 that.getmima()
                
               }else{
                let price=that.data. price*that.data.number*(that.data.sheets?that.data.sheets:1)+(that.data.radio==2?1:0)-(that.data.jian?thats.data.jian:0)
                console.log(price,that.data.money);
                if(price>that.data.taocan[this.data.taocanindex].price){
                   return  wx.showToast({
                     title: '余额不足',
                     mask:true
                   })
                }
                that.getmima()
               }
               
              
               
             }
           })
           
            
          }else{
            wx.showToast({
              title: res.data.tips,
              icon:'none',
              mask:true
            })
          }
         
         }
         
     
        })
      },1500)


    },
    //获取用户是否设置过密码
    getmima(){
      wx.request({
        url: 'https://zujieba.com/Api/order/pay_pass.html',
        method:"POST",
        data:{uid:wx.getStorageSync('uid')},
        success:(res)=>{
          console.log(res);
          if(res.data.success==1){
            this.setData({show:true})
          }else{
            this.setData({show1:true})
          }
          
        }
      })
    },
     
     
    getxunhuan2(){
      setTimeout(()=>{
        wx.request({
          url: 'https://zujieba.com/Api/Test2/test_print',
          data:{code:this.data.code},
          method:'POST',
          success:(res2)=>{
            console.log(res2);
            var title
          if(res2.data.data.task_state=='READY'){
             title='排队中'
          }else if(res2.data.data.task_state=='PARSING'){
             title='解析中'
          }else if(res2.data.data.task_state=='SENDING'){
            title='发送中'
          }else if(res2.data.data.task_state=='SUCCESS'){
            title='成功'
          }else if(res2.data.data.task_state=='FAILURE'){
            title='失败'
          }
          wx.showLoading({
            title: title,
            mask:true
          })
          console.log(res2);
          if(res2.data.data.task_state=='SUCCESS'){
            wx.hideLoading()
            this.getorderchange()
           
           
           
          }else if(res2.data.data.task_state=='FAILUER'){
            wx.hideLoading()
           
          }else{
            this.getxunhuan2()
          }
    
    
          }
        })
      },1000)
    },
     
     
     //支付
     getpay(){
    wx.requestPayment(
      {
      'timeStamp': this.data.timeStamp,
      'nonceStr': this.data.nonceStr,
      'package': this.data.package,
      'signType': this.data.signType,
      'paySign': this.data.paySign,
      'success':(res)=>{
      let url=''
        console.log(res);
        
              wx.showToast({
                title: '支付成功',
                icon:'success',
                duration:2000,
                mask:true,
                
              })
              if(this.data.flag==17){
                         url=this.data.url
              }
             if(this.data.radio7==1||this.data.radio7==3){

               this.getxunhuan2()
              
             }else{
              wx.navigateTo({
                url: '/pages/canprint/canprint?code='+this.data.code+"&url="+url,
              })
             }

             
             
              this.setData({
                near_id:'',
                goods_id:''
              })
             
        
      },
      fail:(res)=>{
       
      }
      
      })
  },
  //用户选的页数
    getpage(name){
      console.log(this.data.url);
      let num=this.data.value1+"_"+this.data.value2
     

      console.log(name);
      wx.request({
        url:'https://zujieba.com/Api/upload/save_file.html',
        data:{filename:name,num,type:this.data.type},
        method:'POST',
        success:(res)=>{
          console.log(res);
        let url= res.data.data.slice(23)
        url="https://zujieba.com"+url
        console.log(url);
        this.setData({url})
        
        }
      })
    },
    change1(e){
       console.log(e);
       this.setData({
         value1:e.detail.value
       })
     let sheets=this.data.value2-e.detail.value+1
     this.setData({
       sheets
     })
     let price=this.data. price*this.data.number*(this.data.sheets?this.data.sheets:1)+(this.data.radio==2?1:0)
     if(price<this.data.man){
        this.setData({
          jian:0
        })
     }
    
     
      
    },
    change2(e){
      console.log(e);
      this.setData({
        value2:e.detail.value
      })
      let sheets=e.detail.value-this.data.value1+1
      this.setData({
        sheets
      })
      let price=this.data. price*this.data.number*(this.data.sheets?this.data.sheets:1)+(this.data.radio==2?1:0)
      if(price<this.data.man){
         this.setData({
           jian:0
         })
      }
     
      
    },
    //获取用户地址
    getaddress(){
      wx.request({
        url: 'https://zujieba.com/Api/car/address.html',
        data:{uid:wx.getStorageSync('uid')},
        method:'POST',
        success:(res)=>{
          console.log(res);
          if(res.data.success==1){
          let address=res.data.data.province+res.data.data.city+res.data.data.area+res.data.data.address
          this.setData({address,address_name:res.data.data.name,address_phone:res.data.data.phone,address_id:res.data.data.id})}
          else{return}
          
        }
      })
    },

    //跳转到我的地址
    navaddress(){
      wx.navigateTo({
        url: '/pages/mine/address/address',
      })
    },
  close(){
    this.setData({
      show:false
    })
    wx.navigateTo({
      url: '/pages/orderprint/orderprint?change=1',
    })
   
  },
  input(e){
    let str
    if(e.detail.value.length>6){
     
   let value5= e.detail.value.substring(0,6) 
   this.setData({value5})
    }else{
      str=e.detail.value
      this.setData({value5:str})
    }
    console.log(str)
    
    
    let arr=  str.split('')

    console.log(arr);
    
      this.setData({
        passwordvalue:arr
      })
  },
  sureclick(){
    wx.showLoading({
      title: '正在打印...',
      mask:true
    })
    console.log(this.data.near_id,this.data.goods_id);
    
      wx.request({
        url: 'https://zujieba.com/Api/order/pay_order.html',
        data:{paypass:this.data.value5,code:this.data.code},
        method:'POST',
        success:(res)=>{
          if(res.data.success==1){
            this.setData({
              show:false
            })
            let url=''
            if(this.data.flag==17){
              url=this.data.url
            }
          console.log(res);
           if(this.data.radio7==1||this.data.radio7==3){
            wx.request({
              url: 'https://zujieba.com/api/Test2/print.html',
              data:{type:1,status:1,code:this.data.code},
              success:(res1)=>{
                console.log(this.data.code,this.data.package);
                
                if(res1.data.success==1){
                  console.log(res1);
                  
                  wx.request({
                    url: 'https://zujieba.com/api/Test2/print.html',
                    data:{type:2,status:1,code:this.data.code},
                    success:(res)=>{
                      console.log(res);
                      
                      wx.hideLoading()
                      if(res.data.success==1){
                        
                      let task_id=res.data.data.data.task_id
                      let deviceId=res.data.data.order.deviceId
                      let deviceKey=res.data.data.order.deviceKey
                      this.getxunhuan(task_id,deviceId,deviceKey)
                    
                    
                    }else{wx.showToast({
                         title: res.data.tips,
                         icon:'none',
                         mask:true
                       })
                      console.log(res1);
                      }
                    }})
                  
                }else{
                  wx.hideLoading(
                         
                    )
                  wx.showToast({
                    title: res1.data.tips,
                    mask:true
                  })
                }
                
              }
            })
           }else{
            wx.navigateTo({
              url: '/pages/canprint/canprint?code='+this.data.code+"&url="+url,
            })
           }
           
           
              this.setData({
                near_id:'',
                goods_id:''
              })
              this.setData({value5:'',passwordvalue:[]})
              
          }else if(res.data.success==2){
            console.log(res,this);
            wx.showToast({
              title: res.data.tips,
              icon:'none',
              mask:true
            })
            
           
            this.setData({value5:'',passwordvalue:[]})
            console.log(this.data.value5);
            
          }else{
            this.setData({value5:'',passwordvalue:[],show:false,show1:true})
          }
         
          
        },
        fail:(res)=>{
          console.log(res);
          
          wx.showToast({
            title: res.data.tips,
            duration:2000,
            mask:true
          })
          this.setData({value5:''})
        }
      })
  },
  getxunhuan(task_id){
    setTimeout(()=>{
     wx.request({
       url: 'https://zujieba.com/api/Test2/print.html',
       
       data:{task_id,type:3,status:1,code:this.data.code},
       success:(res2)=>{
          console.log(res2);
          
         
         var title
         if(res2.data.data.data.task_state=='READY'){
            title='排队中'
         }else if(res2.data.data.data.task_state=='PARSING'){
            title='解析中'
         }else if(res2.data.data.data.task_state=='SENDING'){
           title='发送中'
         }else if(res2.data.data.data.task_state=='SUCCESS'){
           title='成功'
         }else if(res2.data.data.data.task_state=='FAILURE'){
           title='失败'
         }
         wx.showLoading({
           title: title,
           mask:true
         })
        
         if(res2.data.data.data.task_state=='SUCCESS'){
           this.setData({flag:true})
           
             wx.hideLoading()
             wx.navigateTo({
               url: '/pages/orderprint/orderprint?change=3',
             })
          
          
         }else if(res2.data.data.data.task_state=='FAILUER'){
           wx.hideLoading()
           this.setData({flag:false})
         }else{
           this.getxunhuan(task_id)
         }
        
       }
     })
    },1000)
 },
  //获取用户的余额
  getyuemoney(){
    wx.request({
      url: 'https://zujieba.com/Api/member/member.html',
      data:{uid:wx.getStorageSync('uid')},
      method:'POST',
      success:(res)=>{
        console.log(res);
        this.setData({
           money:res.data.data.price,
           radio1:1
        })
        // if(parseFloat(res.data.data.price)>0&&this.data.typeobj.package==1){
        //   this.setData({radio1:2})
        //   console.log(parseFloat(res.data.data.price));
          
          
          
        // }else{
        //   this.setData({radio1:1})

        // }
        
      }
    })
  },
  //预约打印
   toggle7(e){
    this.setData({
      radio7:e.currentTarget.dataset.radio
    })
   },
  //选择打印方式
     toggle5(e){
        this.setData({
          radio5:e.currentTarget.dataset.radio
        })
     },
     bindPickerChange2(e){
       console.log(e);
       this.setData({
         index2:e.detail.value
       })
       
     },
     toggle6(e){
        this.setData({
          radio6:e.currentTarget.dataset.radio
        })
        this.getpages(e.currentTarget.dataset.radio)
     },
    /* getsuofang(e){
       console.log(e.detail.value);
       
       this.setData({
        suofang:e.detail.value
       })
       console.log(this.data.suofang);
       
         if(this.data.suofang==''){
             this.setData({radio5:0})
             console.log(this.data.radio5);
             
         } 
         console.log(this.data.radio5);
         
         if(this.data.suofang!=''){
             this.setData({radio5:2})
         }
     },*/
     toggle13(e){
        this.setData({
          type:e.currentTarget.dataset.type
        })
        let sheets
        if(this.data.page==''||this.data.page==1){sheets=1}else if(this.data.type==1){
          if(this.data.page%2==0){
            sheets=this.data.page/2
            console.log(1111);
            
          }else{
           sheets=parseInt(this.data.page/2)+1
           console.log(222);
           
          }
        }else if(this.data.type==2){
             
               sheets=parseInt(this.data.page/2)
               console.log(333);
               
             
 
        }else{sheets=this.data.value2-this.data.value1+1}
        this.setData({sheets})
        let price=this.data. price*this.data.number*(this.data.sheets?this.data.sheets:1)+(this.data.radio==2?1:0)
        if(price<this.data.man){
           this.setData({
             jian:0
           })
        }
        /*this.setData({basesheets:sheets})
        let arr=this.data.speclist[1].data
        var title
       arr.forEach((item)=>{
          if(item.checked==true){
            title=item.title
          }
       })
        console.log(arr,title);
        
        
        console.log(title);
        
        if(title=="长边"||title=="短边"){
          this.setData({flag:2})
          if(sheets%2==0){
             sheets=sheets/2
          }else{
            sheets=parseInt(sheets/2)+1
          }
          this.setData({sheets})
        }else{
          this.setData({sheets,flag:1})
        }*/
       
        
     },
     toggle14(){
      
      let sheets
      if(this.data.page==''||this.data.page==1){sheets=1}else if(this.data.type==1){
        if(this.data.page%2==0){
          sheets=this.data.page/2
          console.log(1111);
          
        }else{
         sheets=parseInt(this.data.page/2)+1
         console.log(222);
         
        }
      }else if(this.data.type==2){
           
             sheets=parseInt(this.data.page/2)
             console.log(333);
             
           

      }else{sheets=this.data.value2-this.data.value1+1}
      this.setData({sheets})
      let price=this.data. price*this.data.number*(this.data.sheets?this.data.sheets:1)+(this.data.radio==2?1:0)
      if(price<this.data.man){
         this.setData({
           jian:0
         })
      }
      /*this.setData({basesheets:sheets})
      let arr=this.data.speclist[1].data
      var title
     arr.forEach((item)=>{
        if(item.checked==true){
          title=item.title
        }
     })
      console.log(arr,title);
      
      
      console.log(title);
      
      if(title=="长边"||title=="短边"){
        this.setData({flag:2})
        if(sheets%2==0){
           sheets=sheets/2
        }else{
          sheets=parseInt(sheets/2)+1
        }
        this.setData({sheets})
      }else{
        this.setData({sheets,flag:1})
      }*/
     
      
   },
     //节流
    throttle:function(func,wait){
      console.log(func,wait);
      
      let last =this.data.last
      console.log(last);
      
      let current_time= +new Date()
      if(current_time-last>wait){
        console.log(1111);
          console.log(current_time,current_time-last);
          
          func()
          last=+new Date()
          this.setData({
            last
          })
      }
        
         
      
       
    },
    navpaypass(){
      this.setData({show1:false})
      wx.navigateTo({
        url: '/pages/mine/paypass/paypass?phone='+wx.getStorageSync('phone'),
      })
   },
   shanchu(){
    this.setData({
      show1:false
    })
  },
  onHide(){
    this.setData({show:false,show1:false})
  },
  jianclick1(){
     let num=this.data.number1
     if(num==1){
       return
     }
     num--
     this.setData({number1:num})
  },
  addclick1(){
    let num=this.data.number1
    if(num==100){
      return
    }
    num++
    this.setData({number1:num})
  },
  // 获取店铺余额的套餐
   gettaocan(){
        wx.request({
          url: 'https://zujieba.com/Api/store/member_price',
          data:{goods_id:this.data.goods_id,uid:wx.getStorageSync('uid')},
          method:'POST',
          success:(res)=>{
            console.log(res);
            this.setData({
              taocan:res.data.data.data,
              msg:res.data.data.msg
            })
          }
        })
   },
   toggle4(e){
     this.setData({
       taocanindex:e.currentTarget.dataset.taocanindex
     })
   },
   //告诉金耀 订单状态 需要改变
   getorderchange(){
     console.log(this.data.code);
     
      wx.request({
        url: 'https://zujieba.com/Api/Test2/order_change',
        data:{code:this.data.code},
        method:'POST',
        success:(res)=>{
          console.log(res);
          wx.navigateTo({
            url: '/pages/orderprint/orderprint?change=3',
          })
        }
      
      })
   }
    
   
  
  
})